﻿using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace WPFStyleAndBehavior
{
    public class DragInCanvasBehaviors : Behavior<UIElement>
    {
        private Canvas canvas;

        /// <summary>
        /// 这个是相对AssociatedObject的初始点
        /// </summary>
        private Point startPoint;

        private bool isDragging;
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown), true);
            this.AssociatedObject.AddHandler(UIElement.MouseMoveEvent, new MouseEventHandler(AssociatedObject_MouseMove), true);
            this.AssociatedObject.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp), true);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.AssociatedObject.RemoveHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown));
            this.AssociatedObject.RemoveHandler(UIElement.MouseMoveEvent, new MouseEventHandler(AssociatedObject_MouseMove));
            this.AssociatedObject.RemoveHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp));
        }

        /// <summary>
        /// 单击鼠标左键按下时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (canvas == null)
            {
                canvas = (VisualTreeHelper.GetParent(this.AssociatedObject) as Canvas)!;
            }

            startPoint = e.GetPosition(this.AssociatedObject);
            Console.WriteLine($"X:{startPoint.X};Y:{startPoint.Y}");
            isDragging = true;

            this.AssociatedObject.CaptureMouse();
        }

        /// <summary>
        /// 鼠标移动时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                Point point = e.GetPosition(canvas);
                Console.WriteLine($"X:{point.X};Y:{point.Y}");
                AssociatedObject.SetValue(Canvas.TopProperty, point.Y - startPoint.Y);
                AssociatedObject.SetValue(Canvas.LeftProperty, point.X - startPoint.X);
            }
        }

        /// <summary>
        /// 单击鼠标左键按下再松开时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDragging)
            {
                isDragging = false;
                this.AssociatedObject.ReleaseMouseCapture();
            }
        }
    }
}
